package com.neusoft.tmall.aspect;

import com.macro.mall.common.api.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class CommonResultControllerAspect {
    /*
        1. 定义切入点
        2. 实现环绕
     */
    @Pointcut("execution(public * com.neusoft.tmall.ums.controller..*.*(..)) ")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object aroundAuthAround(ProceedingJoinPoint pjp) throws Throwable {
        log.info("aroundAuthAround");
//        Method method = getMethod(pjp);
//        System.out.println(method);
        Object result = pjp.proceed();
        System.out.println(result);
        CommonResult commonResult = CommonResult.success(result);
        if(result instanceof Map){
            Map map = (Map) result;
            if(map.containsKey("msg")){
                String msg = String.valueOf(map.get("msg"));
                map.remove("msg");
                commonResult = CommonResult.success(result, msg);
            }
        }
        return commonResult;
    }

    private Method getMethod(ProceedingJoinPoint pjp){
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        return methodSignature.getMethod();
    }

}
